<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 订单管理
 * @author zxj
 *
 */
class Aorder_new extends MY_Controller {
	function __construct() {
		parent::__construct();
	}


    /**
     * 返修单统计
     */
    function reworkOrder(){
        $sql = "select a.id,a.order_num,a.source_oid,a.status_id,a.rework_orderNum,a.consumer_email,a.merchant_num,a.create_time,
                b.technician,a.point
                from mt_order as a
                left join mt_product as b on a.order_num = b.order_num
                where a.service_type=4";
        $orders = $this->common_model->getQuery($sql,'result_array');
        foreach ($orders as &$rs) {
            $pr = substr($rs['rework_orderNum'], 0, 2);
            $rs['oldTechnician'] = '系统无法核实,需要人工观察';
            $rs['oldCreate_time'] = '系统无法核实,需要人工观察';
            if(!$rs['rework_orderNum'])continue;
            if ($pr == 'wx') {
                $sql = '
                select a.technician as oldTechnician,b.create_time as oldCreate_time from mt_product as a
                left join mt_order as b  on a.oid=b.id
                where a.order_num = \''.$rs['rework_orderNum'].'\'';
            }else{
                $sql = '
                select a.add_time as oldCreate_time,a.modified_user as oldTechnician from maintain_track as a
                where a.maintain_id = \''.$rs['rework_orderNum'].'\' and a.target_status=3';
            }
            $reworkOrder = $this->common_model->getQuery($sql,'row_array');
            $rs['oldTechnician'] = $reworkOrder['oldTechnician'];
            $rs['oldCreate_time'] = $reworkOrder['oldCreate_time'];
        }
        unset($rs);
        $fieldData = array(
            'order_num'=>'返修单号','source_oid'=>'来源单号','status_id'=>'状态ID',
            'rework_orderNum'=>'维修单号','consumer_email'=>'客户邮箱','merchant_num'=>'商家编号',
            'create_time'=>'返修单创建日期','technician'=>'返修技术人员','point'=>'站点',
            'oldTechnician'=>'维修技术人员','oldCreate_time'=>'维修日期'
        );
        excel_output('reworkOrder'.date('YmdH'),$fieldData,$orders);
    }


    /**
     * 处理维修项
     */
    function clParts(){
        $parts = $this->common_model->selectCustom('mt_customer_price',array('grade'=>3,'category_id'=>3));
        foreach ($parts as $rs) {
            $inData = array(
                'site_id'=>$rs['site_id'],
                'category_id'=>$rs['category_id'],
                'customer_id'=>$rs['customer_id'],
                'grade'=>$rs['grade'],
                'price'=>$rs['price'],
                'base_price'=>$rs['base_price'],
                'labor_id'=>341,
            );
            $this->common_model->insert('mt_customer_price',$inData);
        }

    }

    /**
     * 匹配财务数据
     */
    function cwYml()
    {
        $vals = array('RMAWX','.','150004531','WX16072654481019','DEVES00005807','AGENCIA','RECOLHAEMAIL0408','WX16080410199535','WX16080510299989','WX16080456989954','RAQUEL','RAQUELGAN','EMAIL','WX16081255505054','.','BALCAO','.','WX16081754514857','150004710','WX16082556529898','150004689','150004736','.','NºDEV-150004767');
        foreach ($vals as $val) {
            $val = strtolower(trim($val));
            $putVal = '';
            if (!$val) {
                echo 'Null<br/>';
                continue;
            }
            $pr = substr($val, 0, 2);
            if ($pr == 'wx') {
                $where = array('order_num' => $val);
                if (strpos($val, '-')) {
                    $val = substr(strstr($val, '-', true), 2);
                    $where = array('id' => $val);
                }
                $orders = $this->common_model->selectFirst('mt_order', $where);
                $customerDetail = $this->common_model->selectFirst('mt_customers', array('id' => $orders['merchant_num']));
                $putVal = $customerDetail['name']?$customerDetail['name']:$orders['consumer_email'];
            } elseif ($pr == 'es') {
                $orders = $this->common_model->selectFirst('maintain_order', array('maintain_id' => $val));
                $customerDetail = $this->common_model->selectFirst('mt_customers', array('email' => $orders['consumer_email']));
                $putVal = $customerDetail['name'] ? $customerDetail['name'] : $orders['consumer_email'];
            }
            if ($putVal) {
                echo $putVal, '<br>';
            } else {
                echo 'Null<br/>';
            }

        }

    }


}
